import {login,getUserProfile} from '../../utils/asyncWx'
import {request} from '../../request/index'

Page({

  data: {
    user: {},
    collectNum: 0
  },

 onShow() {
  // 获取缓存中用户的信息
  let user = wx.getStorageSync('user')
  // 获取缓存收藏商品的数量
  let collect = wx.getStorageSync('collect') || []

  // 获取 user 中的参数发送请求获取token
  // const {encryptedData, rawData, iv, signature} = user
  // console.log(encryptedData,rawData,iv,signature)

  
  this.setData({
    user,
    collectNum: collect.length
  })
 },

  // 获取用户信息
  async getUserProfile() {
    let user = await getUserProfile({desc: '用于登录使用'})
    let {code} = await login()
    const {encryptedData, rawData, iv, signature} = user
    const loginParams = {encryptedData, rawData, iv, signature, code}

    //发送请求获取 token
    let res = await request({
      url: '/users/wxlogin',
      method: 'POST',
      data: loginParams
    })

    console.log(res);

    //把用户信息放进缓存中
    wx.setStorageSync('user', user.userInfo)
    this.onShow()
  }

})